using Avalonia.BiliBili.Libs.Models.View;
using Avalonia.BiliBili.Units;
using Avalonia.BiliBili.Windows.ViewModels;
using Avalonia.Controls;
using Avalonia.Extensions.Event;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Article;
using Bilibili.Model.Common;
using Bilibili.WebClient.Api;
using DynamicData;
using PCLUntils.Objects;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Avalonia.BiliBili.Windows
{
    public partial class ArticlePartWindow : WindowBase
    {
        private ArticleClient Client { get; }
        private ArticlePartViewModel ViewModel { get; }
        public ArticlePartWindow()
        {
            InitializeComponent();
            Client = new ArticleClient();
            ViewModel = new ArticlePartViewModel();
        }
        protected override void AfterInitializeComponent()
        {
            this.DataContext = ViewModel;
        }
        public override void Callback()
        {
            try
            {
                if (Data is Category category)
                {
                    Title = category.Content;
                    RunOnUiThread(async () =>
                    {
                        var result = await Client.GetCategories();
                        if (result.IsSuccess())
                        {
                            var obj = result.Data;
                            if (obj.IsNotEmpty())
                            {
                                if (obj["code"].ToInt32() == 0)
                                {
                                    var array = obj["data"].ToObject<List<ArticleCategories>>().Where(x => x.Id == category.Id);
                                    var items = array.Select(x => new TabControlItem<Articles>
                                    {
                                        Header = x.Name,
                                        Type = x.Children.Select(x => new Category { Content = x.Name, Tag = x.Id })
                                    });
                                    ViewModel.Items.AddRange(items);
                                }
                            }
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("Callback", ex);
            }
        }
        public void OnItemClick(object sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickItem is ListBoxItem item && item.Content is Articles article)
                {
                    string url = "https://www.bilibili.com/read/app/" + article.Id;
                    Navigater.OpenWebView(url, true);
                }
            }
            catch { }
        }
        public void OnTypeSelect(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var comboBox = (ComboBox)sender;
                if (comboBox.SelectedItem is Category category)
                {
                    RunOnUiThread(async () =>
                    {
                        var result = await Client.GetRecommends(category.Tag.ToInt32(), 1);
                        if (result.IsSuccess())
                        {
                            var obj = result.Data;
                            if (obj.IsNotEmpty())
                            {
                                if (obj["code"].ToInt32() == 0)
                                {
                                    if (obj["data"] != null)
                                    {
                                        var ls = obj["data"].ToObject<List<Articles>>();
                                        if (ls.Count != 0)
                                        {
                                            var item = ViewModel.Items.FirstOrDefault(x => x.Id == comboBox.Tag.ToString());
                                            item?.Nodes.AddRange(ls);
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("OnTypeSelect", ex);
            }
        }
    }
}